<?php
/**
 * 45消化、泌尿、血液内分泌、代谢-3
 */
require_once('util/txt2array.php');
require_once('util/array2csv.php');
header("content-type:text/html;charset=utf-8");

$name = "src45-3";
$line = txt2Array("question/{$name}.txt");

$line = array_values(array_filter($line, "trim"));

foreach ($line as &$val) {
    $val = trim($val);
}

$line = str_replace("\t", "", $line);
//$stem = preg_replace('/_{3,20}/', "_____", $stem);


$data = [];
$type = ""; //题型
$stem = ""; //题干
$operation = ""; //选项
$answer = "";   //答案
$score = "2";
$level = "3";
$courseCode = '="00018001"';


//处理data
foreach ($line as $item) {
    if (in_array($item, ["单选题", "多选题"])) {
        if ($item == '单选题') {
            $score = 1;
        } else {
            $score = 2;
        }
        $type = $item;
        continue;
    }

    if (preg_match("/^[0-9]{1,3}[.．、]/", $item)) {
        $levelNum = ["偏易" => 2, "偏难" => 4, "易" => 1, "难" => 5,];
        $levelArray = ["(易","（易", "(偏易","（偏易", "(偏难","（偏难", "(难","（难", "（第"];
        foreach ($levelArray as $levelLabel) {
            $stemTmp = explode($levelLabel, $item);
            if (!empty($stemTmp[1])) {
                $levelStr = $levelLabel . $stemTmp[1];
                foreach ($levelNum as $k => $v) {
                    if (strpos($levelStr, $k) !== false) {
                        $level = $v;
                        break;
                    }
                }
                break;
            }
        }

        $stem = $stemTmp[0];
        $stem = preg_replace("/^[0-9]{1,3}(．)?(、)?[.．、]?/", "", $stem);
        $stem = str_replace("(1.0分）", "", $stem);
        $stem = str_replace("(1.0分)", "", $stem);
        $stem = str_replace("(1.0 分)", "", $stem);
        $stem = str_replace("(1.0 分）", "", $stem);
        $stem = str_replace("(1.0分", "", $stem);
        continue;
    }
    if (strpos($item, "A") === 0
        || strpos($item, "B") === 0
        || strpos($item, "C") === 0
        || strpos($item, "D") === 0
        || strpos($item, "E") === 0
        || strpos($item, "F") === 0
        || strpos($item, "G") === 0
        || strpos($item, "Ａ") === 0
        || strpos($item, "Ｂ") === 0
        || strpos($item, "Ｃ") === 0
        || strpos($item, "Ｄ") === 0
    ) {
        $operation .= $item;
        continue;
    }

    if (strpos($item, "答案：") === 0 || strpos($item, "参考答案：") === 0|| strpos($item, "答案:") === 0|| strpos($item, "答案") === 0) {
        $anstmp = explode("答案：", $item);
        if(empty($anstmp[1])){
            $anstmp = explode("答案:", $item);
        }
        if(empty($anstmp[1])){
            $anstmp = explode("答案", $item);
        }
        $answer = $anstmp[1];
        $answer = implode(";", str_split($answer));
    }

    if (!empty($type) && !empty($stem) && !empty($operation) && !empty($answer)) {
        $operation = Operation::splitOptions($operation);
        $stem = trim($stem);

        $data[] = [$type, "", $stem, $operation, $answer, "", $level, $score, $courseCode];
        $stem = ""; //题干
        $operation = ""; //选项
        $answer = "";   //答案
    } else {
        $stem .= $item;
    }


}


$file_name = "util\csv\{$name}";
toCSV($file_name, $data, getHeaders());